<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="HealthTeaForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="大众化功能定制" v-bind="validateInfos.functions" id="HealthTeaForm-functions" name="functions">
								<a-input v-model:value="formData.functions" placeholder="请输入大众化功能定制"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否怕冷/怕风/怕热/无" v-bind="validateInfos.cold" id="HealthTeaForm-cold" name="cold">
								<a-input v-model:value="formData.cold" placeholder="请输入是否怕冷/怕风/怕热/无"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否爱出汗" v-bind="validateInfos.sweat" id="HealthTeaForm-sweat" name="sweat">
								<a-input v-model:value="formData.sweat" placeholder="请输入是否爱出汗"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="头疼头晕" v-bind="validateInfos.headache" id="HealthTeaForm-headache" name="headache">
								<a-input v-model:value="formData.headache" placeholder="请输入头疼头晕"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="大便频率" v-bind="validateInfos.stoolFreq" id="HealthTeaForm-stoolFreq" name="stoolFreq">
								<a-input v-model:value="formData.stoolFreq" placeholder="请输入大便频率"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="大便形态" v-bind="validateInfos.stoolShape" id="HealthTeaForm-stoolShape" name="stoolShape">
								<a-input v-model:value="formData.stoolShape" placeholder="请输入大便形态"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否有尿频" v-bind="validateInfos.urineFreq" id="HealthTeaForm-urineFreq" name="urineFreq">
								<a-input v-model:value="formData.urineFreq" placeholder="请输入是否有尿频"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="小便通畅程度" v-bind="validateInfos.urineFlow" id="HealthTeaForm-urineFlow" name="urineFlow">
								<a-input v-model:value="formData.urineFlow" placeholder="请输入小便通畅程度"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="小便颜色" v-bind="validateInfos.urineColor" id="HealthTeaForm-urineColor" name="urineColor">
								<a-input v-model:value="formData.urineColor" placeholder="请输入小便颜色"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="胃口如何" v-bind="validateInfos.appetite" id="HealthTeaForm-appetite" name="appetite">
								<a-input v-model:value="formData.appetite" placeholder="请输入胃口如何"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="偏好食物温度" v-bind="validateInfos.foodTemp" id="HealthTeaForm-foodTemp" name="foodTemp">
								<a-input v-model:value="formData.foodTemp" placeholder="请输入偏好食物温度"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="胸部感觉，多选，用逗号分隔" v-bind="validateInfos.chest" id="HealthTeaForm-chest" name="chest">
								<a-input v-model:value="formData.chest" placeholder="请输入胸部感觉，多选，用逗号分隔"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="胸部不适程度" v-bind="validateInfos.chestLevel" id="HealthTeaForm-chestLevel" name="chestLevel">
								<a-input v-model:value="formData.chestLevel" placeholder="请输入胸部不适程度"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否口渴、口干" v-bind="validateInfos.thirst" id="HealthTeaForm-thirst" name="thirst">
								<a-input v-model:value="formData.thirst" placeholder="请输入是否口渴、口干"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否有耳鸣" v-bind="validateInfos.tinnitus" id="HealthTeaForm-tinnitus" name="tinnitus">
								<a-input v-model:value="formData.tinnitus" placeholder="请输入是否有耳鸣"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="听力情况" v-bind="validateInfos.hearing" id="HealthTeaForm-hearing" name="hearing">
								<a-input v-model:value="formData.hearing" placeholder="请输入听力情况"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="近期疾病" v-bind="validateInfos.recentIllness" id="HealthTeaForm-recentIllness" name="recentIllness">
								<a-input v-model:value="formData.recentIllness" placeholder="请输入近期疾病"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="老毛病" v-bind="validateInfos.oldIllness" id="HealthTeaForm-oldIllness" name="oldIllness">
								<a-input v-model:value="formData.oldIllness" placeholder="请输入老毛病"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="月经是否正常" v-bind="validateInfos.period" id="HealthTeaForm-period" name="period">
								<a-input v-model:value="formData.period" placeholder="请输入月经是否正常"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否有痛经" v-bind="validateInfos.dysmenorrhea" id="HealthTeaForm-dysmenorrhea" name="dysmenorrhea">
								<a-input v-model:value="formData.dysmenorrhea" placeholder="请输入是否有痛经"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="月经周期" v-bind="validateInfos.cycle" id="HealthTeaForm-cycle" name="cycle">
								<a-input v-model:value="formData.cycle" placeholder="请输入月经周期"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="月经颜色" v-bind="validateInfos.color" id="HealthTeaForm-color" name="color">
								<a-input v-model:value="formData.color" placeholder="请输入月经颜色"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否有血块" v-bind="validateInfos.clot" id="HealthTeaForm-clot" name="clot">
								<a-input v-model:value="formData.clot" placeholder="请输入是否有血块"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="手脚是否发凉" v-bind="validateInfos.coldLimbs" id="HealthTeaForm-coldLimbs" name="coldLimbs">
								<a-input v-model:value="formData.coldLimbs" placeholder="请输入手脚是否发凉"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否有麻木感" v-bind="validateInfos.numbness" id="HealthTeaForm-numbness" name="numbness">
								<a-input v-model:value="formData.numbness" placeholder="请输入是否有麻木感"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="入睡是否正常" v-bind="validateInfos.sleep" id="HealthTeaForm-sleep" name="sleep">
								<a-input v-model:value="formData.sleep" placeholder="请输入入睡是否正常"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="通常几点睡觉" v-bind="validateInfos.sleepTime" id="HealthTeaForm-sleepTime" name="sleepTime">
								<a-input v-model:value="formData.sleepTime" placeholder="请输入通常几点睡觉"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="通常几点起床" v-bind="validateInfos.wakeTime" id="HealthTeaForm-wakeTime" name="wakeTime">
								<a-input v-model:value="formData.wakeTime" placeholder="请输入通常几点起床"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="夜间是否会醒" v-bind="validateInfos.wake" id="HealthTeaForm-wake" name="wake">
								<a-input v-model:value="formData.wake" placeholder="请输入夜间是否会醒"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="通常几点醒来（夜间)" v-bind="validateInfos.nightWakeTime" id="HealthTeaForm-nightWakeTime" name="nightWakeTime">
								<a-input v-model:value="formData.nightWakeTime" placeholder="请输入通常几点醒来（夜间)"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否有痰" v-bind="validateInfos.phlegm" id="HealthTeaForm-phlegm" name="phlegm">
								<a-input v-model:value="formData.phlegm" placeholder="请输入是否有痰"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="痰的颜色" v-bind="validateInfos.phlegmColor" id="HealthTeaForm-phlegmColor" name="phlegmColor">
								<a-input v-model:value="formData.phlegmColor" placeholder="请输入痰的颜色"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="嗓子是否发痒" v-bind="validateInfos.throat" id="HealthTeaForm-throat" name="throat">
								<a-input v-model:value="formData.throat" placeholder="请输入嗓子是否发痒"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否流鼻涕" v-bind="validateInfos.runnyNose" id="HealthTeaForm-runnyNose" name="runnyNose">
								<a-input v-model:value="formData.runnyNose" placeholder="请输入是否流鼻涕"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="鼻涕颜色" v-bind="validateInfos.snotColor" id="HealthTeaForm-snotColor" name="snotColor">
								<a-input v-model:value="formData.snotColor" placeholder="请输入鼻涕颜色"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="其他不舒服的地方" v-bind="validateInfos.otherDiscomfort" id="HealthTeaForm-otherDiscomfort" name="otherDiscomfort">
								<a-input v-model:value="formData.otherDiscomfort" placeholder="请输入其他不舒服的地方"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="您主要想解决的问题" v-bind="validateInfos.mainProblem" id="HealthTeaForm-mainProblem" name="mainProblem">
								<a-input v-model:value="formData.mainProblem" placeholder="请输入您主要想解决的问题"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="手机号" v-bind="validateInfos.phone" id="HealthTeaForm-phone" name="phone">
								<a-input v-model:value="formData.phone" placeholder="请输入手机号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="回复" v-bind="validateInfos.reply" id="HealthTeaForm-reply" name="reply">
								<a-textarea v-model:value="formData.reply" placeholder="请输入回复"  allow-clear ></a-textarea>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../HealthTea.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    functions: '',   
    cold: '',   
    sweat: '',   
    headache: '',   
    stoolFreq: '',   
    stoolShape: '',   
    urineFreq: '',   
    urineFlow: '',   
    urineColor: '',   
    appetite: '',   
    foodTemp: '',   
    chest: '',   
    chestLevel: '',   
    thirst: '',   
    tinnitus: '',   
    hearing: '',   
    recentIllness: '',   
    oldIllness: '',   
    period: '',   
    dysmenorrhea: '',   
    cycle: '',   
    color: '',   
    clot: '',   
    coldLimbs: '',   
    numbness: '',   
    sleep: '',   
    sleepTime: '',   
    wakeTime: '',   
    wake: '',   
    nightWakeTime: '',   
    phlegm: '',   
    phlegmColor: '',   
    throat: '',   
    runnyNose: '',   
    snotColor: '',   
    otherDiscomfort: '',   
    mainProblem: '',   
    phone: '',   
    reply: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
